﻿using System.Data.SqlClient;
using AdoDAL.Entities;
using AdoDAL.Repositories;
//using Stiline.DAL.Entities;
//using Stiline.DAL.Repositories;
using Stiline.Web.Models.Task;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Mvc;
using System.Web.Security;

namespace Stiline.Web.Controllers
{
    public class TaskApiController : Controller
    {
        // GET api/taskapi\
        [Authorize]
        public JsonResult Get(int? userId)
        {
            try
            {
                var currentUser = Membership.GetUser() as User;
                var user = Membership.GetUser() as User;
                if (userId.HasValue)
                {
                    var userRepository = new UserRepository();
                    user = userRepository.Get(userId.Value);
                }
                if (user == null || currentUser == null)
                {
                    return null;
                }
                var isOwner = user.Id.Equals(currentUser.Id);
                var taskRepository = new TaskRepository();
                var tasks =
                    taskRepository.GetAll()
                                  .Where(t => t.UserId.Equals(user.Id) && (t.IsPublic || isOwner))
                                  .ToList()
                                  .Select(t => new
                                      {
                                          id = t.Id,
                                          start = t.StartTime,
                                          end = t.EndTime,
                                          title = t.Title,
                                          body = t.Description,
                                          userId = t.UserId,
                                          isPublic = t.IsPublic
                                      });
                return Data(tasks);
            }
            catch (SqlException e)
            {
                return Data(null, false);
            }
        }

        [Authorize]
        public JsonResult Post(TaskModel task)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var userRepository = new UserRepository();
                    var currentUser = Membership.GetUser() as User;
                    if (task.Id == 0)
                    {
                        task.UserId = currentUser.Id;
                    }
                    var user = userRepository.Get(task.UserId);
                    if (currentUser.Id == user.Id)
                    {
                        var taskRepository = new TaskRepository();
                        var newTask = new Task
                            {
                                Id = task.Id,
                                Title = task.Title,
                                Description = task.Description,
                                StartTime = task.Start,
                                EndTime = task.End,
                                UserId = currentUser.Id,
                                IsPublic = task.IsPublic
                            };
                        if (newTask.Description == null)
                        {
                            newTask.Description = "";
                        }
                        taskRepository.Save(newTask);
                    }
                }
                catch (SqlException e)
                {
                    return Data("Problem with database", false);
                }
            }
            return Data(null);
        }


        // DELETE api/taskapi/5
        [HttpPost]
        public void Delete(int? id)
        {
            if (!id.HasValue)
            {
                return;
            }
            var taskRepository = new TaskRepository();
            var userRepository = new UserRepository();

            var currentUser = Membership.GetUser() as User;
            var task = taskRepository.Get(id.Value);
            if (task == null)
            {
                return;
            }
            var user = userRepository.Get(task.UserId);

            if (currentUser.Id.Equals(user.Id))
            {
                if (task.UserId.Equals(user.Id))
                {
                    taskRepository.Delete(id.Value);
                }
            }
        }

        public JsonResult Data(object data, bool success = true)
        {
            var responce = new
                {
                    data = data,
                    success = success
                };
            return Json(responce, JsonRequestBehavior.AllowGet);
        }
    }
}
